// Copyright 2010, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.adwords.lib.utils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * {@code UnitsUsageMap} represents a mapping of account name to number of
 * units used by the account and all of its children;
 * {@link UnitsUsageMap#getUnitsUsageMap()} is used to retrieve this mapping.
 * Because accounts can be linked to two separate parents (through UI/API and
 * just API), there is a chance that an account can be doubly counted by the
 * same parent further up in the account hierarchy;
 * {@link UnitsUsageMap#getDoubleCountedChildren()} is used to retrieve these
 * accounts.
 *
 * @author api.arogal@gmail.com (Adam Rogal)
 */
public class UnitsUsageMap {
  private String rootAccount;
  private Map<String, Long> unitsUsage;
  private List<String> doubleCountedChildren;
  private Long sumTotalUsage;

  /**
   * @param rootAccount the root account name below which all children's quota
   *     is mapped
   * @param unitsUsage a {@code Map<String, Long>} mapping of account to total
   *     API units used by it and its children
   * @param doubleCountedChildren children which may have been double counted
   * @param sumTotalUsage the sum of all children's usage below the root account
   */
  public UnitsUsageMap(String rootAccount, Map<String, Long> unitsUsage,
      List<String> doubleCountedChildren, Long sumTotalUsage) {
    this.rootAccount = rootAccount;
    this.unitsUsage = unitsUsage;
    this.doubleCountedChildren = doubleCountedChildren;
    this.sumTotalUsage = sumTotalUsage;
  }


  /**
   * Returns the name of the root account below which all children have been
   * mapped.
   *
   * @return the name of the root account
   */
  public String getRootAccount() {
    return rootAccount;
  }

  /**
   * Returns the sum total of all children below the root account.
   *
   * @return the sum total of all children below the root account
   */
  public Long getSumTotalUsage() {
    return sumTotalUsage;
  }

  /**
   * Returns an unmodifiable {@code Map<String, Long>} which maps account to
   * total usage of its children.
   *
   * @return a mapping of child account name to api quota usage
   */
  public Map<String, Long> getUnitsUsageMap() {
    return Collections.unmodifiableMap(unitsUsage);
  }

  /**
   * Returns an unmodifiable {@code List<String>} which lists all children
   * which may have been doubly counted.
   *
   * @return all double counted children
   */
  public List<String> getDoubleCountedChildren() {
    return Collections.unmodifiableList(doubleCountedChildren);
  }
}
